import 'package:flutter/material.dart';

class CommonGridView<T> extends StatelessWidget {
  final List<T> items;
  final int crossAxisCount; // 每行多少列
  final double mainAxisSpacing;
  final double crossAxisSpacing;
  final double childAspectRatio;
  final EdgeInsetsGeometry padding;
  final Widget Function(BuildContext context, T item, int index) itemBuilder;

  const CommonGridView({
    super.key,
    required this.items,
    required this.itemBuilder,
    this.crossAxisCount = 3,
    this.mainAxisSpacing = 10,
    this.crossAxisSpacing = 10,
    this.childAspectRatio = 1.0,
    this.padding = const EdgeInsets.all(12),
  });

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: padding,
       shrinkWrap: true, // 关键
      physics: const BouncingScrollPhysics(), 
      itemCount: items.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: crossAxisCount,
        mainAxisSpacing: mainAxisSpacing,
        crossAxisSpacing: crossAxisSpacing,
        childAspectRatio: childAspectRatio,
      ),
      itemBuilder: (context, index) {
        final item = items[index];
        return itemBuilder(context, item, index);
      },
    );
  }
}